package com.resou.reader.bookdetail.adapter;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.resou.reader.R;
import com.resou.reader.ReSouApplication;
import com.resou.reader.api.entry.ChapterItemBean;
import com.resou.reader.bookshelf.listener.OnItemClickListener;
import com.resou.reader.reader.utils.SpUtils;
import com.resou.reader.utils.PreferenceUtil;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * Created by ${Garrett} on 2018/9/4.
 * Contact me krouky@outlook.com
 */
public class CatalogueAdapter extends RecyclerView.Adapter<CatalogueAdapter.ViewHolder> {

    private List<ChapterItemBean> mList = null;

    private OnItemClickListener listener;
    private String type;

    // 选中项红色
    private int selectedItem = 0;
    private boolean reversed;

    public void setType(String type) {
        this.type = type;
    }

    public void setList(List<ChapterItemBean> list) {
//        mList.clear();
//        mList.addAll(list);
        mList = list;
        if (reversed) {
            Collections.reverse(mList);
        }
        notifyDataSetChanged();
//        no
    }

    public CatalogueAdapter(OnItemClickListener listener) {
        this.listener = listener;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        return new ViewHolder(LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.catalogue_item_layout, viewGroup, false));
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
        if (mList == null) {
            return;
        }
        ChapterItemBean bean = mList.get(i);
        viewHolder.catalogueText.setText(bean.getChapterName());
        if ("cata".equals(type)) {
            if ("0".equals(bean.getIsVip()) || "1".equals(bean.getIsVipUser())) {//免费付费
                viewHolder.rightImage.setVisibility(View.GONE);
                if (bean.isCached()) {
                    viewHolder.rightText.setText("已下载");
                } else {
                    viewHolder.rightText.setText("未下载");
                }

            } else {//章节付费
                if ("1".equals(bean.getIsPay())) {//已付费
                    if (bean.isCached() && 0 == PreferenceUtil.getInstance(ReSouApplication.getRSApplication()).getSubSidyStateValue()) {
                        viewHolder.rightImage.setVisibility(View.GONE);
                        viewHolder.rightText.setText("已下载");
                    } else {
                        viewHolder.rightText.setText("");
                        viewHolder.rightImage.setVisibility(View.VISIBLE);
                        viewHolder.rightImage.setImageResource(R.mipmap.lock_open);
                    }
                } else {
                    viewHolder.rightText.setText("");//未付费
                    viewHolder.rightImage.setVisibility(View.VISIBLE);
                    viewHolder.rightImage.setImageResource(R.mipmap.lock_close);
                }
            }
        } else {
            int tempSelectedItem = selectedItem;
            if (reversed) {
                tempSelectedItem = mList.size() - 1 - selectedItem;
            }
            if (i == tempSelectedItem) {
                viewHolder.catalogueText.setTextColor(viewHolder.itemView.getResources().getColor(R.color.theme_color));
                viewHolder.rightText.setTextColor(viewHolder.itemView.getResources().getColor(R.color.theme_color));
                if ("0".equals(bean.getIsVip()) || "1".equals(bean.getIsVipUser())) {//免费付费
                    viewHolder.rightImage.setVisibility(View.GONE);
                    if (bean.isCached()) {
                        viewHolder.rightText.setText("已下载");
                    } else {
                        viewHolder.rightText.setText("未下载");
                    }

                } else {//章节付费
                    if ("1".equals(bean.getIsPay())) {//已付费
                        if (bean.isCached() && 0 == PreferenceUtil.getInstance(ReSouApplication.getRSApplication()).getSubSidyStateValue()) {
                            viewHolder.rightImage.setVisibility(View.GONE);
                            viewHolder.rightText.setText("已下载");
                        } else {
                            viewHolder.rightText.setText("");
                            viewHolder.rightImage.setVisibility(View.VISIBLE);
                            viewHolder.rightImage.setImageResource(R.mipmap.lock_open);
                        }
                    } else {
                        viewHolder.rightText.setText("");//未付费
                        viewHolder.rightImage.setVisibility(View.VISIBLE);
                        viewHolder.rightImage.setImageResource(R.mipmap.lock_close);
                    }
                }
                if (SpUtils.getTheme(viewHolder.itemView.getContext())) {
                    viewHolder.viewLine.setBackgroundColor(viewHolder.itemView.getResources().getColor(R.color.color_333));
                } else {
                    viewHolder.viewLine.setBackgroundColor(viewHolder.itemView.getResources().getColor(R.color.color_F4F4F4));
                }
            } else {
                if (SpUtils.getTheme(viewHolder.itemView.getContext())) {
                    viewHolder.catalogueText.setTextColor(viewHolder.itemView.getResources().getColor(R.color.color_999));
                } else {
                    viewHolder.catalogueText.setTextColor(viewHolder.itemView.getResources().getColor(R.color.color_333));
                }
                viewHolder.rightText.setTextColor(viewHolder.itemView.getResources().getColor(R.color.color_666));

                if (SpUtils.getTheme(viewHolder.itemView.getContext())) {
                    viewHolder.viewLine.setBackgroundColor(viewHolder.itemView.getResources().getColor(R.color.color_333));
                } else {
                    viewHolder.viewLine.setBackgroundColor(viewHolder.itemView.getResources().getColor(R.color.color_F4F4F4));
                }
                if ("0".equals(bean.getIsVip()) || "1".equals(bean.getIsVipUser())) {//免费付费
                    viewHolder.rightImage.setVisibility(View.GONE);
                    if (bean.isCached()) {
                        viewHolder.rightText.setText("已下载");
                    } else {
                        viewHolder.rightText.setText("未下载");
                    }
                    if (SpUtils.getTheme(viewHolder.itemView.getContext())) {
                        viewHolder.catalogueText.setTextColor(viewHolder.itemView.getResources().getColor(R.color.color_999));
                        viewHolder.rightText.setTextColor(viewHolder.itemView.getResources().getColor(R.color.color_999));
                    } else {
                        viewHolder.catalogueText.setTextColor(viewHolder.itemView.getResources().getColor(R.color.color_333));
                        viewHolder.rightText.setTextColor(viewHolder.itemView.getResources().getColor(R.color.color_333));
                    }
                } else {//章节付费
                    if ("1".equals(bean.getIsPay())) {//已付费
                        if (bean.isCached() && 0 == PreferenceUtil.getInstance(ReSouApplication.getRSApplication()).getSubSidyStateValue()) {
                            viewHolder.rightImage.setVisibility(View.GONE);
                            viewHolder.rightText.setText("已下载");
                        } else {
                            viewHolder.rightText.setText("");
                            viewHolder.rightImage.setVisibility(View.VISIBLE);
                            viewHolder.rightImage.setImageResource(R.mipmap.lock_open);
                        }
                    } else {
                        viewHolder.rightText.setText("");//未付费
                        viewHolder.rightImage.setVisibility(View.VISIBLE);
                        viewHolder.rightImage.setImageResource(R.mipmap.lock_close);
                    }
                    if (SpUtils.getTheme(viewHolder.itemView.getContext())) {
                        viewHolder.catalogueText.setTextColor(viewHolder.itemView.getResources().getColor(R.color.color_666));
                    } else {
                        viewHolder.catalogueText.setTextColor(viewHolder.itemView.getResources().getColor(R.color.color_999));
                    }
                }
            }
        }

    }

    @Override
    public int getItemCount() {
        return mList == null ? 0 : mList.size();
    }

    public void reverse(RecyclerView recyclerView, int visibleChildCount) {
        Collections.reverse(mList);
        reversed = !reversed;
        notifyDataSetChanged();
        recyclerView.postDelayed(new Runnable() {
            @Override
            public void run() {
                if (reversed) {
                    recyclerView.scrollToPosition(0);
                } else {
                    recyclerView.scrollToPosition(selectedItem + visibleChildCount - 1);
                }
            }
        }, 0);

    }

    public void scroollToPosition(RecyclerView recyclerView, int visibleChildCount) {
        recyclerView.postDelayed(new Runnable() {
            @Override
            public void run() {
                if (reversed) {
                    reversed = !reversed;
                    Collections.reverse(mList);
                    notifyDataSetChanged();
                    recyclerView.scrollToPosition(selectedItem + visibleChildCount - 1);
                }
            }
        }, 0);
    }

    public void reverse() {
        Collections.reverse(mList);
        reversed = !reversed;
        notifyDataSetChanged();
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        TextView catalogueText;
        ImageView rightImage;
        TextView rightText;
        View viewLine;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            catalogueText = itemView.findViewById(R.id.catalogue_item_text);
            rightImage = itemView.findViewById(R.id.right_image);
            rightText = itemView.findViewById(R.id.right_text);
            viewLine = itemView.findViewById(R.id.viewLine);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (listener != null) {
                        listener.onItemClicked(CatalogueAdapter.this, v, getAdapterPosition());
                    }
                }
            });
        }
    }

    public void clear() {
        if (mList != null) {
            mList.clear();
        }
        notifyDataSetChanged();
    }

    public void setSelectedItem(int selectedItem) {
        if (selectedItem != -1 && selectedItem < getItemCount()) {
            notifyItemChanged(selectedItem);
        }
        this.selectedItem = selectedItem;
//        if (selectedItem != -1 && selectedItem < getItemCount()) {
//            notifyItemChanged(selectedItem);
//        }
    }
}
